﻿using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Web.Script.Serialization;

/// <summary>
/// Сводное описание для PageX
/// </summary>
public class PageX : Page
{

    public NameValueCollection _REQUEST;

    public PageX()
    {
        System.Web.HttpRequest Request = HttpContext.Current.ApplicationInstance.Request;
        _REQUEST = (Request.RequestType == "GET") ? Request.QueryString : Request.Form;

    }

    public struct unity
    {
        public int value;
        public string name;
    };

    public struct jresult
    {
        public jresult(int a, string b, object c = null)
        {
            errcode = a; errmess = b; data = c;
        }
        public int errcode;
        public string errmess;
        public object data;
    };

    public struct jqGridData
    {
        public int page;
        public int total;
        public int records;
        public struct row
        {
            public int id;
            public string[] cell;
        };
        public row[] rows;
    };

    // Упростим вывод
    public void echo(string mess) { this.Response.Write(mess); }

    public string json_encode(object anyo)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string jsonresult = serializer.Serialize(anyo);
        return jsonresult;
    }

    public object json_dencode(string ser, Type oType)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        object obj = serializer.Deserialize(ser, oType);
        return obj;
    }

    public string quote(int val) { return "'" + val.ToString() + "'"; }
    public string quote(double val) { return "'" + val.ToString() + "'"; }
    public string quote(string val) { return "'" + val.Replace("'", "''") + "'"; }

    public void json_response(int errcode, string errmess, object data = null)
    {
        Response.AddHeader("Content-type", "text/script; charset=utf-8");
        Response.Write( json_encode( new jresult( errcode, errmess, data )) );
    }


    public bool str_IsUInt32(string s)
    {
        if (s == null || s == "") return false;
        return System.Text.RegularExpressions.Regex.IsMatch(
            s, "^[0-9]+$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    }
    public bool str_IsReal(string s)
    {
        if (s == null || s == "") return false;
        return System.Text.RegularExpressions.Regex.IsMatch(
            s, "^[+-]?\\d+(?:[.,]?\\d+)?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    }
    public bool str_IsHex(string s)
    {
        if (s == null || s == "") return false;
        return System.Text.RegularExpressions.Regex.IsMatch(
            s, "^[0-9a-f]+$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    }
    public bool str_IsColorCode(string s)
    {
        if (s == null || s == "") return false;
        return System.Text.RegularExpressions.Regex.IsMatch(
            s, "^[0-9a-f]{6}$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    }
}
